import { Body, Controller, Post, Req, Headers, Res } from '@nestjs/common';
import { errorReturn, successReturn } from 'src/util';
import { RechargeService } from './recharge.service';

@Controller('recharge')
export class RechargeController {
    constructor(
        private rechargeService:RechargeService
    ) {}
    @Post()
    async recharge(@Body('amount') amount:number, @Req() { userId, clientSource }) {
        amount = Number(amount)
        if(isNaN(amount) || amount <= 0) return errorReturn()
        try {
            let res = await this.rechargeService.recharge(amount, userId, clientSource)
            if(res) {
                return successReturn(res)
            }
        } catch (error) {
            
        }

        return errorReturn()
    }

    @Post('notify') 
    async notify(@Body() body:any, @Headers() headers, @Res() response) {
        try {
            const res = await this.rechargeService.rechargeNotify(body, headers)
            if(res === true) {
                response.status(200).send({   
                    "code": "SUCCESS",
                    "message": "成功"
                })

                return
            }

            if(res === undefined) return

        } catch (error) {
            console.log(error.message)
        }

        response.status(500).send({   
            "code": "FAIL",
            "message": "失败"
        })

    }
}
